<?php

class Util_Setup {
	
	/**
	 * Metoda pobiera parametr setup - array jesli więcej wpisów.
	 * @return array|string|null
	 */
	static public function get($param){
		$db = Zend_Db_Table::getDefaultAdapter();
		$q = $db->query($db->select()
			->from('setup')
			->where("setup_name like '$param'")
		);
		switch($q->rowCount()){
			case 0: return null;
			case 1: return $q->fetchObject()->setup_value;
			default:
				$arr = array();
				while($c = $q->fetchObject()){
					array_push($arr, $c->setup_value);
				}
				return $arr;
		}
	}
	
	static public function set($param,$value){
		$setup = new Zend_Db_Table('setup');
		$result = $setup->update(array("setup_value"=>$value), "setup_name = '$param'");
		if($result == 0){
			$result = $setup->insert(array("setup_value"=>$value, "setup_name"=>$param));
		}
	}
	
	static public function add($data){
		$setup = new Zend_Db_Table('setup');
		return $setup->insert($data);
	}
	
	
	static public function registry($var,$val = '<magic_null>'){
		if($val ==='<magic_null>'){
			return Zend_Registry::get($var);
		} else {
			Zend_Registry::set($var,$val);
		}
	}
	
	static public function config($cfgs = null){
		if(is_object($cfgs)){
			self::registry('my_config',$cfgs);
		} else if(is_string($cfgs)){
			return self::registry('my_config')->$cfgs;
		} else {
			return self::registry('my_config');
		}
	}
	
	
}